<template></template>

<script>
import {
  getnewdata,
  deletnewdata,
  updatenewdata,
  addenewdata,
} from "@/api/new.js";
export default {
  data() {
    return {
      newdata: [],
      page: 1, //分页器当前第几页
      limit: 5, //每一页需要展示几条数据
      total: 0,
      somedata: [],
      nowitem: {},
      dialogTableVisible: false,
      drawer: false,
      form: {},
      dialogImageUrl: "",
      dialogVisible: false,
      flag: 0, //0更新 1添加
    };
  },
  mounted() {
    this.initdata();
  },

  methods: {
    async initdata() {
      let result = await getnewdata();

      if (result.status == 0) {
        for (let i = 0; i < result.data.length; i++) {
          result.data[i].like = JSON.parse(result.data[i].like);
        }
        // 遍历数组A中的每个对象B
        result.data.forEach((item) => {
          // 初始化变量来保存value的总和
          let sum = 0;
          // 初始化变量来保存like数组的长度
          let length = 0;

          // 遍历对象B中的like数组
          item.like.forEach((likeItem) => {
            // 累加value的值
            sum += likeItem.value;
            // 增加like数组的长度
            length++;
          });

          // 检查like数组是否有元素，以避免除以0的错误
          if (length > 0) {
            // 计算平均值并保存在对象B中
            item.averageLikeValue = sum / length;
          }
        });
        console.log(result.data);
        // this.newdata = result.data;
        this.total = this.newdata.length;
        this.somedata = this.newdata.slice(
          (this.page - 1) * this.limit,
          (this.page - 1) * this.limit + this.limit
        );
        this.$store.dispatch("news/savedata", this.newdata);
      }
    },
  },
};
</script>

<style>
</style>